/*
 * Copyright (c) 2020 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.plattysoft.leonids.examples.slice;

import com.plattysoft.leonids.ParticleSystem;
import com.plattysoft.leonids.examples.ResourceTable;
import com.plattysoft.leonids.examples.utils.ResUtil;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.animation.Animator;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.element.ShapeElement;
import ohos.multimodalinput.event.TouchEvent;

public class FollowCursorExampleAbilitySlice extends AbilitySlice implements Component.TouchEventListener {
    private ComponentContainer mRootId;
    private ParticleSystem ps;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        ComponentContainer rootLayout = (ComponentContainer) LayoutScatter.getInstance(getContext()).parse(ResourceTable.Layout_main_empty, null, false);
        mRootId = (ComponentContainer) rootLayout.findComponentById(ResourceTable.Id_parentid);
        ShapeElement background_color_element = new ShapeElement();
        background_color_element.setShape(ShapeElement.RECTANGLE);
        background_color_element.setRgbColor(new RgbColor(0, 0, 0));
        background_color_element.setBounds(0, 0, mRootId.getRight(), mRootId.getBottom());
        mRootId.setBackground(background_color_element);
        mRootId.setTouchEventListener(this);
        super.setUIContent(rootLayout);
    }

    @Override
    public boolean onTouchEvent(Component component, TouchEvent touchEvent) {
        switch (touchEvent.getAction()) {
            case TouchEvent.PRIMARY_POINT_DOWN:
                // Create a particle system and start emiting
                ps = new ParticleSystem(mRootId, 25, ResUtil.getPixelMapElementByResId(getContext(), ResourceTable.Media_star_pink), 800);
                ps.setScaleRange(0.7f, 1.3f);
                ps.setSpeedRange(0.05f, 0.1f);
                ps.setRotationSpeedRange(90, 180);
                ps.setFadeOut(200, Animator.CurveType.ACCELERATE);
                ps.emit((int) touchEvent.getPointerScreenPosition(0).getX(), (int) touchEvent.getPointerScreenPosition(0).getY(), 15);
                break;
            case TouchEvent.POINT_MOVE:
                ps.updateEmitPoint((int) touchEvent.getPointerScreenPosition(0).getX(), (int) touchEvent.getPointerScreenPosition(0).getY());
                break;
            case TouchEvent.PRIMARY_POINT_UP:
                ps.stopEmitting();
                break;
        }
        return true;
    }
}
